//
//  CacheDataLayer.m
//  MCF3
//
//  Created by QianDeng on 15/8/14.
//  Copyright (c) 2015年 ac. All rights reserved.
//

#import "CacheDataLayer.h"
#import "NSArray+MutableDeepCopy.h"
#import "NSDictionary+MutableDeepCopy.h"

@implementation CacheDataLayer

//获取序列化存储文件
+(id) getSingleObjByName:(NSString *)Name
{
    id Data = nil;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if([defaults objectForKey:Name]){
        if ([[defaults objectForKey:Name] isKindOfClass:[NSData class]]) {
            Data = [NSKeyedUnarchiver unarchiveObjectWithData:[defaults objectForKey:Name]];
        }else if([[defaults objectForKey:Name] isKindOfClass:[NSArray class]]){
            NSMutableArray * List = [[NSMutableArray alloc] init];
            NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:Name]];
            for (int i = 0; i < [array count]; i ++) {
                [List addObject:[NSKeyedUnarchiver unarchiveObjectWithData:[array objectAtIndex:i]]];
            }
            Data = List;
        }
        else
        {
            Data = [defaults objectForKey:Name];
        }
        
    }
    return Data;
}
//保存序列化存储文件
+ (BOOL) saveSingleObj:(id)Data byName:(NSString *)Name
{
    id dispatchData = nil;
    if (Data) {  //如果存储空的数据，不需要做拷贝
        if ([Data isKindOfClass:[NSArray class]]) {
            dispatchData = [Data mutableDeepCopy];  //对数组进去object深拷贝;
        }
        else if ([Data isKindOfClass:[NSDictionary class]]) {
            dispatchData = [Data mutableDeepCopy];  //对词典进去object深拷贝
        }
        else
        {
            if ([Data respondsToSelector:@selector(copy)]) {
                dispatchData = [Data copy];
            }
            else{
                dispatchData = Data;
            }
        }
    }
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:[NSKeyedArchiver archivedDataWithRootObject: dispatchData]forKey:Name];
        [defaults synchronize];
    });
    return YES;
}

//移除沙盒序列化文件
+ (void)removeSingleObj:(NSString *)name
{
    if (!name) {
        return;
    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:name];
}

//获取Plist文件
+ (NSDictionary *)getPlistData:(NSString *)name
{
    if (!name) {
        return nil;
    }
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *plist = [path objectAtIndex:0];
    NSString *fileName = [plist stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", name]];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:fileName];
    return data;
}

//保存PLIST文件
+ (void)saveDataToPlist:(NSDictionary*)dic fileName:(NSString *)name
{
    if (!dic || !name) {
        return;
    }
    // 沙盘目录
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *plist = [path objectAtIndex:0];
    NSString *fileName = [plist stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", name]];
    NSMutableDictionary *userData = [[NSMutableDictionary alloc] initWithDictionary:dic];
    [userData writeToFile:fileName atomically:YES];
}

//移除PLIST文件
+ (void)removePlistData:(NSString *)name
{
    if (!name) {
        return;
    }
    // 沙盘目录
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *plist = [path objectAtIndex:0];
    NSString *fileName = [plist stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", name]];
    [[NSFileManager defaultManager] removeItemAtPath:fileName error:nil];
}

//获取Plist文件
+ (NSDictionary *)getPlistDataWithFileName:(NSString *)fName keyName:(NSString *)kName
{
    if (!fName || !kName) {
        return nil;
    }
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *plist = [path objectAtIndex:0];
    NSString *fileName = [plist stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", fName]];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:fileName];
    NSDictionary *dict = [data objectForKey:kName];
    return dict;
}

//保存PLIST文件
+ (void)saveDataToPlist:(NSDictionary*)dic fileName:(NSString *)fName keyName:(NSString *)kName
{
    if (!dic || !fName || !kName) {
        return;
    }
    // 沙盘目录
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *plist = [path objectAtIndex:0];
    NSString *fileName = [plist stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", fName]];
    if ([[NSFileManager defaultManager] fileExistsAtPath:fileName] ) {
        NSMutableDictionary *userData = [[NSMutableDictionary alloc] initWithContentsOfFile:fileName];
        [userData setObject:dic forKey:kName];
        [userData writeToFile:fileName atomically:YES];
    }
    else{
        // 创建
        NSMutableDictionary *userData = [[NSMutableDictionary alloc]init];
        [userData setObject:dic forKey:kName];
        [userData writeToFile:fileName atomically:YES];
    }
}

@end
